package cn.com.utt.unms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.unms.pojo.City;
import cn.com.utt.unms.pojo.District;
import cn.com.utt.unms.pojo.Province;
import cn.com.utt.unms.service.AddressService;

@RestController
@RequestMapping("v1/address")
public class AddressController {

	@Autowired
	private AddressService addressService;

	@RequestMapping(value = "/loadProvince", method = { RequestMethod.GET })
	public Map<String, Object> loadProvince(HttpServletRequest request, HttpServletResponse response) {
		RestResult result = new RestResult();
		String serverName = request.getServerName();
		if (serverName.equals("cloud.transliteglobal.com")){
			List<Province> list = new ArrayList<Province>();
			result.put("province", list);
			return result.toJsonObject();
		}
		List<Province> list = addressService.queryAllProvinces();
		result.put("province", list);
		return result.toJsonObject();
	}

	@RequestMapping(value = "/loadDistrictByCity", method = { RequestMethod.GET })
	public Map<String, Object> loadDistrictByCity(HttpServletRequest request, HttpServletResponse response,Long id) {
		RestResult result = new RestResult();
		List<District> list = addressService.getDistrictByCityId(id);
		result.put("district", list);
		return result.toJsonObject();
	}

	@RequestMapping(value = "/loadCityByProvince", method = { RequestMethod.GET })
	public Map<String, Object> loadCityByProvince(Long id) {
		RestResult result = new RestResult();
		List<City> list = addressService.getCityByProvinceId(id);
		result.put("city", list);
		return result.toJsonObject();
	}

}